<?php
namespace app\web\controller;
use app\common\controller\Api;
use think\Db;

/**
 * 投票控制器
 * @package app\index\controller
 */
class Vote extends Home
{
    public $noNeedLogin = [];

    /**
     * 我的投票
     */
    public function lists(){
        if($this->request->isGet()){
            $getData= $this->request->get();
            $limit = empty($getData['limit'])?10:$getData['limit'];//默认分页10条
            $type = empty($getData['type'])?1:$getData['type'];//默认分页10条
            $userInfo = $this->userInfo;
            //全部投票
            if($type == '1'){
                $data = Db::name('qt_vote')
                    ->field('id,title,start_time,end_time,people_type,created_time,look')
                    ->where('status','1')
                    ->order('id desc')
                    ->paginate($limit);
                $this->result('查询成功',$data,200);
            }elseif($type == '2'){
                $data = Db::name('qt_vote_log')
                    ->alias('vl')
                    ->leftJoin('qt_vote v','vl.vote_id = v.id')
                    ->field('v.id,v.title,v.start_time,v.end_time,v.people_type,v.created_time,v.look')
                    ->where('v.status','1')
                    ->where('vl.user_id',$userInfo['id'])
                    ->order('vl.vote_id desc')
                    ->paginate($limit);
                $this->result('查询成功',$data,200);
            }else{
                $this->result('参数错误','',204);
            }
        }
        $this->result('无效的请求','',204);

    }

    /**
     * 投票详情
     */
    public function detail(){
        if($this->request->isGet()){
            $getData= $this->request->get();
            if(empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $data = Db::name('qt_vote')->where('id',$getData['id'])->find();
            Db::name('qt_vote')->where('id',$getData['id'])->setInc('look');
            if(empty($data)){
                $this->result('查询成功',$data,200);
            }
            $choice = Db::name('qt_vote_choice')->where('vote_id',$data['id'])->select();
            foreach ($choice as $key=>$value){
                $choice[$key]['title']  = str_replace(array("\r\n", "\r", "\n"), "", $value['title']);
            }
            //判断是否投票了
            $count = Db::name('qt_vote_log')->where(['vote_id'=>$getData['id'],'user_id'=>$userInfo['id']])->count();
            $data['choice'] = $choice;
            if($count > 0){
                $data['is_choice'] = 1;
            }else{
                $data['is_choice'] = 0;
            }
            $this->result('查询成功',$data,200);
        }
    }

    /**
     * 投票结果
     */
    public function analysis(){
        if($this->request->isGet()){
            $getData= $this->request->get();
            if(empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            //查找所有的投票选项
            $choiceRes = Db::name('qt_vote_choice')->field('id,title')->where('vote_id',$getData['id'])->select();
            //dump($choiceRes);die;
            //总的投票数
            $totalVote = Db::name('qt_vote_log')->where('vote_id',$getData['id'])->count();
            foreach ($choiceRes as $key=>$value){
                $choiceRes[$key]['title']  = str_replace(array("\r\n", "\r", "\n"), "", $value['title']);
                //单个投票数
               $choiceCount = Db::name('qt_vote_log')->where('vote_id',$getData['id'])->where('vote_choice_id',$value['id'])->count();
                $choiceRes[$key]['count'] = $choiceCount;
                //单个投票比例
                if($totalVote == '0'){
                    $choiceRes[$key]['percentage'] = '0';
                }else{
                    $percentage = $choiceCount / $totalVote;
                    $choiceRes[$key]['percentage'] = sprintf("%.2f",$percentage);
                }

            }
            $data = Db::name('qt_vote')->where('id',$getData['id'])->find();
            $data['total'] = $totalVote;
            $data['choice'] = $choiceRes;
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);
    }


    /**
     * 参加投票
     */
    public function add(){
        if($this->request->isPost()){
            $getData= $this->request->post();
            if(empty($getData['vote_id']) || empty($getData['vote_choice_id'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $find = Db::name('qt_vote_log')->where(['user_id'=>$userInfo['id'],'vote_id'=>$getData['vote_id']])->find();
            if(!empty($find)){
                $this->result('你已经投过票了','',204);
            }
            if(is_array($getData['vote_choice_id'])){
                foreach ($getData['vote_choice_id'] as $key=>$value){
                    $insertData[$key]['user_id'] = $userInfo['id'];
                    $insertData[$key]['vote_id'] = $getData['vote_id'];
                    $insertData[$key]['vote_choice_id'] = $getData['vote_choice_id'];
                }
            }else{
                $insertData[0]['user_id'] = $userInfo['id'];
                $insertData[0]['vote_id'] = $getData['vote_id'];
                $insertData[0]['vote_choice_id'] = $getData['vote_choice_id'];
            }

            //插入数据
            try{
                Db::startTrans();
                //修改订单状态
                Db::name('qt_vote_log')->insertAll($insertData);
                Db::name('qt_vote')->where('id',$getData['vote_id'])->setInc('user_num','1');
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->result('投票失败','',204);
            }
            $this->result('投票成功','',200);
        }
    }




}
